package objects.info;

import objects.ObjectCodes;

import com.google.gson.Gson;

import utils.Shell;
import utils.Utils;

public class JobStatusInfo implements ObjectCodes {
	public int nodes;
	public int procs;
	public int walltime;
	public int freenodes;
	public int freeprocs;
	public int activejobs;
	public int queuedjobs;
	public int queuetime;
	public int date;
	public int time;
	public long datetime;
	
	
	public JobStatusInfo(){}
	public JobStatusInfo( int nn, int p, int wt)
	{
		this.nodes = nn;
		this.procs = p;
		this.walltime = wt;
		date = new java.sql.Date(new java.util.Date().getTime()).getDay();
		time = new java.sql.Time(new java.util.Date().getTime()).getHours();
		datetime = new java.util.Date().getTime();
		try{
			if(!Utils.MOCK) {
				setClusterVars();
			}
		}catch(Exception e){
			try{
				Thread.sleep(3000);
				setClusterVars();
			}catch(Exception ex){activejobs=0;freenodes=0;freeprocs=0;queuedjobs=0;}
		}
			
	}
	
	public void setQueuetime(int q){ this.queuetime = q;}
	
	public void setClusterVars() throws Exception
	{
		String [] dat = Shell.runOneCmd();
		activejobs = Integer.parseInt(dat[0]);
		queuedjobs = Integer.parseInt(dat[1]);
		String nodes = dat[3];
		freenodes = Integer.parseInt(nodes.split("/")[1]) - Integer.parseInt(nodes.split("/")[0]);
		String procs = dat[2];
		freeprocs = Integer.parseInt(procs.split("/")[1]) - Integer.parseInt(procs.split("/")[0]);
	}
	
	public String toString()
	{
		return CLU_JOB_INFO+"|"+ new Gson().toJson(this);
	}
	public static JobStatusInfo fromJSon(String json)
	{
		return new Gson().fromJson(json,JobStatusInfo .class);
	}
}
